package com.example.demo.web;

import com.example.demo.VO.ClazzExtra;
import com.example.demo.core.Result;
import com.example.demo.core.ResultGenerator;
import com.example.demo.model.Clazz;
import com.example.demo.service.ClazzService;
import com.example.demo.service.impl.ClazzServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Kevin
 * @date 2020/4/19 17:01
 */
@RestController
@RequestMapping("/clazz")
public class ClazzController {
    @Resource
    private ClazzService clazzService;

    @PostMapping("/add")
    public Result add(Clazz user) {
        clazzService.save(user);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        clazzService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(Clazz user) {
        clazzService.update(user);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        Clazz user = clazzService.findById(id);
        return ResultGenerator.genSuccessResult(user);
    }

    @PostMapping("/detailMore")
    public Result detailMore(@RequestParam Integer id) {
        ClazzExtra clazz = clazzService.queryClazzInfoWithStudent(id);
        return ResultGenerator.genSuccessResult(clazz);
    }

    @PostMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<Clazz> list = clazzService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

}
